const util = require("../../../utils/util");
const common = require("../../../utils/common");
const requestUtil = require("../../../utils/request");
const api = require("../.././../config/api");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    companyId: null,
    picUrl: api.picUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 安卓有效：隐藏分享按钮
    util.hideShareMenu();
    if(options.q) {
        let params = common.getQueryParams(options.q)
        this.setData({
            companyId: params.cid,
        })
    }else {
        this.setData({
            companyId: options.cid
        })
    }
    if(null == app.globalData.isXcxLogin) {
        this.dictType();
    }
    this.login();
  },

  onShow: function () {
    this.setData({
        headImg: app.globalData.headImg
    })
  },
  async dictType() {
    let that = this;
    let res = await requestUtil.request("GET", api.urls.dictType + 'xcx_login_status', null);
    that.setData({
        isXcxLogin: parseInt(res.data.data.status) == 0 ? true : false,
    })
  },
  /**
   * 获取用户手机号
   */
  getPhoneNumber: async function (e) {
    let _this = this;
    let params = {};
    let sessionKey = wx.getStorageSync('session_key')
    params.sessionkey = sessionKey;
    params.ivdata = e.detail.iv;
    params.encrypdata = e.detail.encryptedData;
    let res = await requestUtil.request("POST", api.urls.getPhone, params);
    let phone = res.data.purePhoneNumber;
    _this.setData({
        phone: phone
    })
  },

  /**
   * login重新获取session_key
   */
  login: function () {
    if(app.globalData.employeeInfo != null) {
        wx.switchTab({
            url: '/pages/my/index/index',
        })
        return;
    }
    let that = this;
    wx.login({
      success: async function (res) {
        let init = await requestUtil.request("POST", api.urls.initUserInfo, {
          code: res.code,
          userType: 3
        });
        that.setData({
            openid: init.data.data.openid
        })
        wx.setStorage({
          key: 'openid',
          data: init.data.data.openid
        });
        wx.setStorage({
          key: "session_key",
          data: init.data.data.session_key
        });
        if(init.data.data.userInfo.length > 0) {
            app.queryInfoByOpenid(init.data.data.openid);
            wx.showToast({title: '您已创建名片', icon: 'success',duration: 2000 })
            setTimeout(function () {
                wx.switchTab({
                  url: '/pages/my/index/index',
                })
            },2000);
        }
      }
    })
  },

  /**
   * 确认表单
   */
  submitForm: async function (e) {
    if(!this.data.xieyiCheck) {
        wx.showToast({title: '请先同意协议', icon: 'none',duration: 1000 })
        return;
    }
    let form = e.detail.value;
    form.companyId = this.data.companyId;
    form.xcxOpenid = this.data.openid;
    form.headImgUrl = app.globalData.headImg;
    let flag = common.formValidate(form, 1);
    if (!flag) {
      return;
    }
    let res = await requestUtil.request("POST", api.urls.addUser, form);
    if (res.data.code != 0) {
        util.showToast(res.data.msg, 'none');
        return;
    }
    app.queryInfoByOpenid(form.xcxOpenid);
    wx.showToast({title: '创建成功', icon: 'success',duration: 3000 })
    setTimeout(function () {
        wx.switchTab({
          url: '/pages/my/index/index',
        })
    },3000);
  },

  /**
   * 重新获取用户信息
   */
  getUserInfo: function (openid) {
    app.queryInfoByOpenid(openid);
  },

  /**
   * 输入手机号input触发事件
   */
  inputPhoneNumber: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  /**
   * 跳转到裁剪图片页面
   */
  toCropper: function () {
    wx.navigateTo({
      url: '/pages/card-create-modify/cropper/cropper',
    })
  },
  xieyiChoose: function() {
    this.setData({
      xieyiCheck: this.data.xieyiCheck ? false : true
    })
  },
  toXieyi: function(e) {
    let idx = e.currentTarget.dataset.type;
    wx:wx.navigateTo({
      url: '/pages/xieyi/xieyi?idx=' + idx
    })
  },

})